package com.haofengsoft.scce.adapter.information;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.haofengsoft.scce.R;
import com.haofengsoft.scce.db.bean.AttentionItem;

public class ManageAttentionAdapter extends ArrayAdapter<AttentionItem> {

    private Context context;
    private List<AttentionItem> modelList;
    private LayoutInflater infater = null;
    private int resource;

    public ManageAttentionAdapter(Context context, int resource,
                                  List<AttentionItem> modelList) {
        super(context, resource, modelList);
        this.context = context;
        this.modelList = modelList;
        infater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.resource = resource;

    }

    @Override
    public int getCount() {
        return modelList.size();
    }

    public List<AttentionItem> getModelList() {
        return modelList;
    }

    //处理移动的数据
    public void handleItemMove(int fromPosition, int toPosition) {
        modelList.get(fromPosition).setOrderIndex(toPosition);
        modelList.get(toPosition).setOrderIndex(fromPosition);
    }

    public void setModelList(List<AttentionItem> modelList) {
        this.modelList = modelList;
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(int position, View convertview, ViewGroup arg2) {

        View view = null;
        AddAttentionHolder holder = null;
        if (convertview == null || convertview.getTag() == null) {
            view = infater.inflate(resource, null);
            holder = new AddAttentionHolder();
            holder.attentionCb = (CheckBox) view
                    .findViewById(R.id.attention_cb);
            holder.titleTv = (TextView) view
                    .findViewById(R.id.attention_title_tv);
            holder.descriptionTv = (TextView) view
                    .findViewById(R.id.attention_description_tv);
            view.setTag(holder);
        } else {
            view = convertview;
            holder = (AddAttentionHolder) convertview.getTag();
        }
        AttentionItem addAttentionModel = (AttentionItem) getItem(position);
        holder.titleTv.setText(addAttentionModel.getAttentionName());

        holder.attentionCb.setOnCheckedChangeListener(new attentionCheckChangeListener(
                position));

//        if (addAttentionModel.getAttentionName().equals(
//                UserAttentionCache.DEFAULT_ATTENTION_TITLE)) {
//            holder.attentionCb.setClickable(false);
//        } else {
//            holder.attentionCb.setClickable(true);
//        }
        holder.descriptionTv.setText(addAttentionModel.getDescription());
        holder.attentionCb.setChecked(addAttentionModel.getAttentionChecked() == 1 ? true : false);
        return view;
    }

    class attentionCheckChangeListener implements OnCheckedChangeListener {

        private int position;

        public attentionCheckChangeListener(int position) {
            this.position = position;
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                                     boolean isChecked) {
            if (isChecked) {
                modelList.get(position).setAttentionChecked(1);
            } else {
                modelList.get(position).setAttentionChecked(0);
            }

        }

    }

    class AddAttentionHolder {
        private CheckBox attentionCb;
        private TextView titleTv;
        private TextView descriptionTv;
    }
}
